package com.hsy.linemoney.apiservice;

import com.deparse.dpframe.tools.utils.DPLogUtil;
import com.hsy.linemoney.R;
import com.hsy.linemoney.requests.RequestTask;
import com.hsy.linemoney.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 服务类型相关
 * Created by MartinKent on 2016/9/12.
 */
public class ServiceTypeService extends BasicService<JSONArray> {
//    获取服务类型列表
    public void getServiceTypeList(final Callback<ServiceTypeListResult> callback, boolean... clearData) {
        if (null != clearData && clearData.length > 0 && clearData[0]) {
            DataHolder.mServiceTypeListResult = null;
        }
        if (null != DataHolder.mServiceTypeListResult) {
            if (null != callback) {
                callback.onSuccess(0, DataHolder.mServiceTypeListResult);
            }
            return;
        }
        final String api = "/LmReferenceServices-portlet.chargetype/get-service-fee-table";
        invoke(0, api, null, new Callback<JSONArray>() {
            @Override
            public void onSuccess(int what, JSONArray result) {
                try {
                    DataHolder.mServiceTypeListResult = new ServiceTypeListResult(result.getJSONObject(0));
                    if (null != callback) {
                        callback.onSuccess(what, DataHolder.mServiceTypeListResult);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    if (null != callback) {
                        callback.onError(what, 0, e.getLocalizedMessage(), e);
                    }
                }
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                if (e instanceof RequestTask.StatusCodeException) {
                    ToastUtil.showToast(msg);
                } else {
                    ToastUtil.showToast(R.string.txt_network_failure);
                }
                if (null != callback) {
                    callback.onError(what, 0, msg, e);
                }
            }
        });
    }

    public static class ServiceTypeListResult {
        private Status status;

        private JSONArray value;

        private List<ServiceType> serviceTypes;

        public ServiceTypeListResult(JSONObject o) throws JSONException {
            Status status = new Status();
            status.setCode(o.getJSONObject("status").getString("code"));
            status.setMessage(o.getJSONObject("status").getString("message"));
            value = o.getJSONArray("value");
        }

        public List<ServiceType> getServiceTypes() {
            return serviceTypes;
        }

        public void setStatus(Status status) {
            this.status = status;
        }

        public Status getStatus() {
            return this.status;
        }

        public void setValue(JSONArray value) {
            this.value = value;
        }

        public JSONArray getValue() {
            return this.value;
        }

        private Pages pages;

        private Map<String, String> serviceTypeMaps;

        private List<String> serviceTypeNames;

        public List<String> getServiceTypeNames() {
            if (null == serviceTypeNames) {
                serviceTypeNames = new ArrayList<>();
                for (ServiceType s : serviceTypes) {
                    serviceTypeNames.add(s.getServiceTypeName());
                }
            }
            return serviceTypeNames;
        }

        public String getServiceTypeCdByName(String name) {
            return serviceTypeMaps.get(name);
        }

        public Pages getPages() {
            if (null == pages) {
                pages = new Pages();
                serviceTypeMaps = new HashMap<>();
                serviceTypes = new ArrayList<>();

                for (int i = 0; i < getValue().length(); i++) {
                    try {
                        JSONObject object = getValue().getJSONObject(i);
                        Iterator<String> iterator = object.keys();
                        List<String> keys = new ArrayList<>();
                        String[] v = new String[2];
                        DPLogUtil.d("yyy " + object.toString());
                        while (iterator.hasNext()) {
                            try {
                                String k = iterator.next();
                                if (isNumeric(k)) {
                                    keys.add(k);
                                } else {
                                    if (k.equalsIgnoreCase("serviceTypeCd")) {
                                        v[1] = object.getString(k);
                                    } else {
                                        v[0] = object.getString(k);
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        serviceTypeMaps.put(v[0], v[1]);
                        serviceTypes.add(new ServiceType(v[0], v[1]));
                        for (String k : keys) {
                            try {
                                Page page;
                                if (pages.hasPage(k)) {
                                    page = pages.getPage(k);
                                } else {
                                    page = new Page();
                                    pages.addPage(k, page);
                                }
                                page.add(new Value(k, v[0], v[1], object.getString(k)));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
            return pages;
        }

        public boolean isNumeric(String in) {
            try {
                Double.valueOf(in);
                return true;
            } catch (Exception e) {
                return false;
            }
        }

        private String[] serviceNameArray;

        public String[] getServiceNameArray() {
            if (null == serviceNameArray) {
                List<String> names = DataHolder.mServiceTypeListResult.getServiceTypeNames();
                serviceNameArray = new String[names.size()];
                for (int i = 0; i < names.size(); i++) {
                    serviceNameArray[i] = names.get(i);
                }
            }
            return serviceNameArray;
        }

        public static class ServiceType {
            private String serviceTypeName;
            private String serviceTypeCd;

            public ServiceType(String serviceTypeName, String serviceTypeCd) {
                this.serviceTypeName = serviceTypeName;
                this.serviceTypeCd = serviceTypeCd;
            }

            public String getServiceTypeName() {
                return serviceTypeName;
            }

            public void setServiceTypeName(String serviceTypeName) {
                this.serviceTypeName = serviceTypeName;
            }

            public String getServiceTypeCd() {
                return serviceTypeCd;
            }

            public void setServiceTypeCd(String serviceTypeCd) {
                this.serviceTypeCd = serviceTypeCd;
            }
        }

        public static class Status {
            private String message;

            private String code;

            public void setMessage(String message) {
                this.message = message;
            }

            public String getMessage() {
                return this.message;
            }

            public void setCode(String code) {
                this.code = code;
            }

            public String getCode() {
                return this.code;
            }
        }

        public static class Pages {
            private List<String> names;
            private Map<String, Page> pages;

            public Pages() {
                names = new ArrayList<String>() {
                    @Override
                    public boolean add(String object) {
                        if (0 == size()) {
                            return super.add(object);
                        } else {
                            if (Double.compare(Double.valueOf(object), Double.valueOf(get(0))) > 0) {
                                super.add(0, object);
                                return true;
                            } else if (Double.compare(Double.valueOf(object), Double.valueOf(get(size() - 1))) < 0) {
                                super.add(object);
                                return true;
                            } else {
                                for (int i = 0; i < size(); i++) {
                                    String v = get(i);
                                    if (Double.compare(Double.valueOf(object), Double.valueOf(v)) > 0) {
                                        super.add(i, object);
                                        return true;
                                    }
                                }
                            }
                        }
                        return false;
                    }
                };
                pages = new HashMap<>();
            }

            public int size() {
                return pages.size();
            }

            public boolean hasPage(String k) {
                return names.contains(k);
            }

            public List<String> getNames() {
                return names;
            }

            public Page getPage(String k) {
                return pages.get(k);
            }

            public Page getPageAt(int index) {
                return pages.get(names.get(index));
            }

            public void addPage(String k, Page page) {
                DPLogUtil.d("addPage " + k);
                if (!names.contains(k)) {
                    DPLogUtil.d("addName name=" + k + ", success=" + names.add(k));
                }
                pages.put(k, page);
            }
        }

        public static class Page extends ArrayList<Value> {
            String name;

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }
        }

        public static class Value {
            private String area;
            private String serviceName;
            private String serviceTypeCd;
            private String amount;

            public Value(String area, String serviceName, String serviceTypeCd, String amount) {
                this.area = area;
                this.serviceName = serviceName;
                this.serviceTypeCd = serviceTypeCd;
                this.amount = amount;
            }

            public String getArea() {
                return area;
            }

            public void setArea(String area) {
                this.area = area;
            }

            public String getServiceName() {
                return serviceName;
            }

            public void setServiceName(String serviceName) {
                this.serviceName = serviceName;
            }

            public String getServiceTypeCd() {
                return serviceTypeCd;
            }

            public void setServiceTypeCd(String serviceTypeCd) {
                this.serviceTypeCd = serviceTypeCd;
            }

            public String getAmount() {
                return amount;
            }

            public void setAmount(String amount) {
                this.amount = amount;
            }
        }
    }
}
